package com.zw.yzk.kabu.data.utils;

import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Map;
import java.util.Set;

/**
 * Created by zhanwei on 2017/2/27.
 */

public class SharedPrefrenceUtils {

    private static SharedPreferences sp;
    private final static String SharePreferncesName = "kabu_sp";

    /**
     * @param context
     * @param key
     * @param value
     * @return 是否保存成功
     */
    public static boolean setValue(Context context, String key, Object value) {
        if (sp == null) {
            sp = context.getSharedPreferences(SharePreferncesName, Context.MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = sp.edit();
        if (value instanceof String) {
            return edit.putString(key, (String) value).commit();
        } else if (value instanceof Boolean) {
            return edit.putBoolean(key, (Boolean) value).commit();
        } else if (value instanceof Float) {
            return edit.putFloat(key, (Float) value).commit();
        } else if (value instanceof Integer) {
            return edit.putInt(key, (Integer) value).commit();
        } else if (value instanceof Long) {
            return edit.putLong(key, (Long) value).commit();
        } else if (value instanceof Set) {
            throw new IllegalArgumentException("Value can not be Set object!");
        }
        return false;
    }

    public static boolean getBoolean(Context context, String key) {
        if (sp == null) {
            sp = context.getSharedPreferences(SharePreferncesName, Context.MODE_PRIVATE);
        }
        return sp.getBoolean(key, false);
    }

    public static String getString(Context context, String key) {
        if (sp == null) {
            sp = context.getSharedPreferences(SharePreferncesName, Context.MODE_PRIVATE);
        }
        return sp.getString(key, "");
    }

    public static Float getFloat(Context context, String key) {
        if (sp == null) {
            sp = context.getSharedPreferences(SharePreferncesName, Context.MODE_PRIVATE);
        }
        return sp.getFloat(key, 0f);
    }

    public static int getInt(Context context, String key) {
        if (sp == null) {
            sp = context.getSharedPreferences(SharePreferncesName, Context.MODE_PRIVATE);
        }
        return sp.getInt(key, 0);
    }

    public static long getLong(Context context, String key) {
        if (sp == null) {
            sp = context.getSharedPreferences(SharePreferncesName, Context.MODE_PRIVATE);
        }
        return sp.getLong(key, 0);
    }

    public static <T> T getObject(Context context, String key, Type type) {
        if (sp == null) {
            sp = context.getSharedPreferences(SharePreferncesName, Context.MODE_PRIVATE);
        }
        return new Gson().fromJson(sp.getString(key, ""), type);
    }

    public static void saveObject(Context context, String key, Object obj) {
        setValue(context, key, new Gson().toJson(obj));
    }

    public static String getLoginAccountKey() {
        return "key_login_account";
    }

    public static String getSplashImageKey() {return "splash_image";}

    public static String getImToken() {
        return "kabu_im_token";
    }

    public static String getMallKey() {
        return "kabu_mall_page";
    }

    public static String getUpdateKey() {
        return "kabu_update";
    }
}
